
package com.kanjian.music.entity;

import org.json.JSONException;
import org.json.JSONObject;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
import com.kanjian.music.KanjianApplication;
import com.kanjian.music.util.CommonUtil;

public class BaseCode {

    @SerializedName("status")
    @Expose
    public int mStatus;

    @SerializedName("error_code")
    public int mErrorCode;

    @SerializedName("sid")
    public String mSid;

    public static BaseCode getCommonCodeFromString(String jsonStr) {
        return KanjianApplication.GSON.fromJson(jsonStr, BaseCode.class);
    }

    protected static JSONObject getJSONObjectFromString(String jsonStr) {
        if (jsonStr != null) {
            try {
                JSONObject jobj = new JSONObject(jsonStr);
                checkSid(jobj);
                return jobj;
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    protected static void checkSid(JSONObject jobj) {
        String sid;
        try {
            sid = jobj.getString("sid");
            if (sid != null && !sid.equals(KanjianApplication.SID)) {
                CommonUtil.saveSessionId(sid);
            }
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    
    public boolean isOKCode() {
        return this.mErrorCode == 20000;
    }

}
